/*
 * Roshan's Robot
 * */
package rs;

import robocode.*;
public class MyStrongRobot extends AdvancedRobot{
	
	final double quarterTurn = 90.0;
    final double halfTurn = 180.0;
    final double threeQuarterTurn = 270.0;
    final double fullTurn = 360.0;
	final double distance = 100;
	final double ticker = 10;
	
	public void run(){
		setAdjustGunForRobotTurn(true);
		setAdjustRadarForGunTurn(false);
		while(true){
			move();
			execute();			
		}
	}	
	public void onScannedRobot(ScannedRobotEvent e) {
		double turnGunAngle= getShortestAngle(getHeading()+e.getBearing()-getGunHeading());
		turnGunRight(turnGunAngle);
		
		if(getEnergy() < 5){
			fire(0.1);
		}
		else if(e.getDistance()<100){
			fire(3);			
		}
		else if(e.getDistance() < 300){
			fire(2);			
		}
		else if(e.getDistance() < 500){
			fire(1);			
			}
		else{
			fire(0.1);
			setTurnRight(quarterTurn);
			execute();
		}
		scan();
	}
	
	public double getShortestAngle(double angle){
		if((angle > - halfTurn) && (angle < halfTurn)){
			return angle; 
		}
		else if(angle < -halfTurn){
			angle += fullTurn;
		}
		else if (angle > halfTurn){
			angle -= fullTurn;
		}
		return angle;
	}
	
	public void onHitByBullet(HitByBulletEvent e) {
		setTurnLeft(90-e.getBearing());
		setAhead(distance/4);
		execute();
		scan();
	}
	
	public void onHitRobot(HitRobotEvent e){
		double turnGunAngle= getShortestAngle(getHeading()+e.getBearing()-getGunHeading());
		turnGunRight(turnGunAngle);
		fire(3);
		setBack(distance/4);
		execute();
		scan();
	}
	
	public void onHitWall(HitWallEvent e){
		setBack(distance/4);
		setTurnRight(quarterTurn);
		setAhead(distance/4);
		execute();
		scan();
	}
	
	public void onBulletHit(BulletHitEvent e){
		setAhead(distance/4);
		scan();
	}
	
	public void onBulletMissed(BulletMissedEvent e){
		scan();
	}
	
	public void move(){
		setTurnRadarRight(fullTurn);			
		setAhead(distance);
		if(getTime()%ticker == 0){
			setTurnRadarRight(fullTurn);
			setBack(distance);
		}
		setTurnRight(quarterTurn);				
	}	
}
